#include "TratamentoRTC.h"

void ajustaMeuRTC(BYTE vetor[]) {
    unsigned int config2, config1;
    /* Baud rate is set for 100 Khz */
    config2 = 0x11;
    /* Configure I2C for 7 bit address mode */
    config1 = (I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_HLD
            & I2C1_IPMI_DIS & I2C1_7BIT_ADD
            & I2C1_SLW_DIS & I2C1_SM_DIS &
            I2C1_GCALL_DIS & I2C1_STR_DIS &
            I2C1_NACK & I2C1_ACK_DIS & I2C1_RCV_DIS &
            I2C1_STOP_DIS & I2C1_RESTART_DIS
            & I2C1_START_DIS);
    OpenI2C1(config1, config2);
    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xD0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(vetor[0]);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(vetor[1]);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(vetor[2]);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(vetor[3]);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(vetor[4]);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(vetor[5]);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(vetor[6]);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    StopI2C1();
    while (I2C1CONbits.PEN);
    CloseI2C1();
}

void printRTC() {
    unsigned int config2, config1;
    /* Baud rate is set for 100 Khz */
    config2 = 0x11;
    /* Configure I2C for 7 bit address mode */
    config1 = (I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_HLD
            & I2C1_IPMI_DIS & I2C1_7BIT_ADD
            & I2C1_SLW_DIS & I2C1_SM_DIS &
            I2C1_GCALL_DIS & I2C1_STR_DIS &
            I2C1_NACK & I2C1_ACK_DIS & I2C1_RCV_DIS &
            I2C1_STOP_DIS & I2C1_RESTART_DIS
            & I2C1_START_DIS);
    OpenI2C1(config1, config2);
    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xD0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    RestartI2C1();
    while (I2C1CONbits.RSEN);
    MasterWriteI2C1(0xD1);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    char aux1;
    aux1 = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    while (I2C1STATbits.ACKSTAT);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    char aux2;
    aux2 = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    while (I2C1STATbits.ACKSTAT);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    char aux3;
    aux3 = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    while (I2C1STATbits.ACKSTAT);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    char aux4;
    aux4 = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    while (I2C1STATbits.ACKSTAT);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    char aux5;
    aux5 = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    while (I2C1STATbits.ACKSTAT);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    char aux6;
    aux6 = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    while (I2C1STATbits.ACKSTAT);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    char aux7;
    aux7 = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    while (I2C1STATbits.ACKSTAT);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    StopI2C1();
    while (I2C1CONbits.PEN);
    Printf("\r\n");
	transformaHora(aux3);
    Printf(":");
    transformaSegMin(aux2);
    Printf(":");
    transformaSegMin(aux1);
    Printf(" ");
    PrintAux(aux4);
    Printf(" ");
    transformaDia(aux5);
    Printf("/");
    transformaMes(aux6);
    Printf("/");
    transformaAno(aux7);
    Printf("\r\n");
    CloseI2C1();
}

void PrintAux(BYTE numero) {
    switch (numero) {
        case 0x00:
            Printf("0");
            break;
        case 0x01:
            Printf("1");
            break;
        case 0x02:
            Printf("2");
            break;
        case 0x03:
            Printf("3");
            break;
        case 0x04:
            Printf("4");
            break;
        case 0x05:
            Printf("5");
            break;
        case 0x06:
            Printf("6");
            break;
        case 0x07:
            Printf("7");
            break;
        case 0x08:
            Printf("8");
            break;
        case 0x09:
            Printf("9");
            break;
    }
}

void transformaAno(char ano) {
    byte aux = (byte) (ano);
    byte aux2;
    byte aux3;
    aux2 = 0xf0;
    aux3 = aux2&aux;
    aux3 = aux3 >> 4;
    PrintAux(aux3);
    aux2 = 0x0f;
    aux3 = aux&aux2;
    PrintAux(aux3);

}

void transformaMes(char mes) {
    byte aux = (byte) (mes);
    byte aux2;
    byte aux3;
    aux2 = 0x10;
    aux3 = aux2&aux;
    aux3 = aux3 >> 4;
    PrintAux(aux3);
    aux2 = 0x0f;
    aux3 = aux&aux2;
    PrintAux(aux3);
}

void transformaDia(char dia) {
    byte aux = (byte) (dia);
    byte aux2;
    byte aux3;
    aux2 = 0x30;
    aux3 = aux&aux2;
    aux3 = aux3 >> 4;
    PrintAux(aux3);
    aux2 = 0x0f;
    aux3 = aux2&aux;
    PrintAux(aux3);

}

void transformaSegMin(char seg) {
    byte aux = (byte) (seg);
    byte aux2;
    byte aux3;
    aux2 = 0x70;
    aux3 = aux&aux2;
    aux3 = aux3 >> 4;
    PrintAux(aux3);
    aux2 = 0x0f;
    aux3 = aux&aux2;
    PrintAux(aux3);
}

void transformaHora(char hora) {
    byte aux = (byte) (hora);
    byte aux2;
    byte aux3;
    aux2 = 0x40;
    aux3 = aux&aux2;
    if (aux3 == 0x40) {

    } else {
        aux2 = 0x30;
        aux3 = aux&aux2;
        aux3 = aux3 >> 4;
        PrintAux(aux3);
        aux2 = 0x0f;
        aux3 = aux2&aux;
        PrintAux(aux3);
    }
}
